/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ADGTFrame.java
 *
 * Created on May 11, 2011, 1:19:10 PM
 */
package tasks.ui;

import common.Observer;
import common.ObserverInterest;
import domain.DomainFacade;
import domain.DomainFacadeImpl;
import javax.swing.JFileChooser;
import tasks.ui.popup.EditNameAuthorFrame;
import tasks.ui.popup.PopupController;
import tasks.ui.popup.PopupControllerImpl;

/**
 *
 * @author David
 */
public class ADGTFrame extends javax.swing.JFrame implements Observer {

    /** Creates new form ADGTFrame */
    public ADGTFrame() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        architecturePanel1 = new tasks.ui.architecture.ArchitecturePanel();
        statusBar1 = new tasks.ui.StatusBar();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        newArchitectureMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        openArchitectureItem = new javax.swing.JMenuItem();
        openStructureItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        saveItem = new javax.swing.JMenuItem();
        saveAsItem = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        editNameAuthorMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Status:");

        jMenu1.setText("Architecture");

        newArchitectureMenuItem.setText("New Architecture");
        newArchitectureMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newArchitectureMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(newArchitectureMenuItem);
        jMenu1.add(jSeparator3);

        openArchitectureItem.setText("Open Architecture");
        openArchitectureItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openArchitectureItemActionPerformed(evt);
            }
        });
        jMenu1.add(openArchitectureItem);

        openStructureItem.setText("Open Structure");
        openStructureItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openStructureItemActionPerformed(evt);
            }
        });
        jMenu1.add(openStructureItem);
        jMenu1.add(jSeparator1);

        saveItem.setText("Save");
        saveItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveItemActionPerformed(evt);
            }
        });
        jMenu1.add(saveItem);

        saveAsItem.setText("Save as");
        saveAsItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAsItemActionPerformed(evt);
            }
        });
        jMenu1.add(saveAsItem);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        editNameAuthorMenuItem.setText("Architecture/Structure name and author");
        editNameAuthorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editNameAuthorActionPerformed(evt);
            }
        });
        jMenu2.add(editNameAuthorMenuItem);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(architecturePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 833, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(636, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(architecturePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(statusBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void openArchitectureItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openArchitectureItemActionPerformed
        int returnVal = jFileChooser1.showOpenDialog(jMenu1);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            adgtController.openArchitectureFile(jFileChooser1.getSelectedFile());
        }
    }//GEN-LAST:event_openArchitectureItemActionPerformed

    private void openStructureItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openStructureItemActionPerformed

        int returnVal = jFileChooser1.showOpenDialog(jMenu1);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            adgtController.openStructureFile(jFileChooser1.getSelectedFile());
        }
    }//GEN-LAST:event_openStructureItemActionPerformed

    private void saveAsItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsItemActionPerformed

        int returnVal = jFileChooser1.showSaveDialog(jMenu1);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            adgtController.saveAs(jFileChooser1.getSelectedFile());
            this.setTitle(this.adgtController.getCurrentArchitectureFileName() + " and " + this.adgtController.getCurrentStructureFileName());
            this.saveItem.setEnabled(true);
            this.statusBar1.setText("Current architecture saved");
        }
    }//GEN-LAST:event_saveAsItemActionPerformed

    private void editNameAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editNameAuthorActionPerformed

        EditNameAuthorFrame editNameAuthor = new EditNameAuthorFrame();
        editNameAuthor.setParentFrame(this);
        this.setEnabled(false);
    }//GEN-LAST:event_editNameAuthorActionPerformed

    private void newArchitectureMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newArchitectureMenuItemActionPerformed

        facade.reset();
        this.dispose();
        new ADGTFrame().setVisible(true);
    }//GEN-LAST:event_newArchitectureMenuItemActionPerformed

    private void saveItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveItemActionPerformed
        adgtController.save();
        this.statusBar1.setText("Current architecture saved");
    }//GEN-LAST:event_saveItemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private tasks.ui.architecture.ArchitecturePanel architecturePanel1;
    private javax.swing.JMenuItem editNameAuthorMenuItem;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JMenuItem newArchitectureMenuItem;
    private javax.swing.JMenuItem openArchitectureItem;
    private javax.swing.JMenuItem openStructureItem;
    private javax.swing.JMenuItem saveAsItem;
    private javax.swing.JMenuItem saveItem;
    private tasks.ui.StatusBar statusBar1;
    // End of variables declaration//GEN-END:variables
    private ADGTController adgtController;
    private PopupController popupController;
    private String title = "Architecture Definition Generation Tool";
    private DomainFacade facade;

    @Override
    public void notifyChange(ObserverInterest interest) {
        if (interest.equals(ObserverInterest.ARCHITECTURE_FILE_OPENED)) {
            this.saveAsItem.setEnabled(true);
            this.openStructureItem.setEnabled(true);
            this.statusBar1.setMessage("Architecture file opened");
            this.setTitle(this.adgtController.getCurrentArchitectureFileName());
        } else if (interest.equals(ObserverInterest.STRUCTURE_FILE_OPENED)) {
            this.saveItem.setEnabled(true);
            this.statusBar1.setMessage("Structure file opened");
            this.setTitle(this.adgtController.getCurrentArchitectureFileName() + " and " + this.adgtController.getCurrentStructureFileName());
        }
    }

    private void init() {
        this.setTitle("Untitled.xml");
        
        this.facade = DomainFacadeImpl.getInstance();
        this.adgtController = new ADGTControllerImpl();
        this.adgtController.addObserver(this, ObserverInterest.ARCHITECTURE_FILE_OPENED);
        this.adgtController.addObserver(this, ObserverInterest.STRUCTURE_FILE_OPENED);
        jFileChooser1.setFileFilter(new XMLFileFilter());
        this.openStructureItem.setEnabled(false);
        this.saveItem.setEnabled(false);
        this.statusBar1.setText("New architecture");
    }

    @Override
    public void setTitle(String title) {
        super.setTitle(this.title + " - " + title);
    }
}
